package com.genesis.yunnanji.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.genesis.yunnanji.MainActivity;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.jaeger.library.StatusBarUtil;
import com.lidroid.xutils.BitmapUtils;

import java.util.Timer;
import java.util.TimerTask;



public class StartAdver extends AppCompatActivity implements View.OnClickListener {
    private ImageView mImageView;
    private TextView mTextView;
    private Timer timer;
    private String imgUrl;
    private String linkUrl;
    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startadver);
        context = this;
        initView(savedInstanceState);
        setUpView();
    }

    protected void initView(Bundle savedInstanceState) {

        StatusBarUtil.setTransparentForImageView(this, mImageView);
        imgUrl = getIntent().getStringExtra("advert_img");
        linkUrl = getIntent().getStringExtra("target_link");
        mImageView = (ImageView) findViewById(R.id.iv_startadver_img);
        mTextView = (TextView) findViewById(R.id.tv_startadver_skip);
        timer = new Timer();
    }

    protected void setUpView() {
        mImageView.setOnClickListener(this);
        mTextView.setOnClickListener(this);
        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.display(mImageView, GenesisApiConfig.PIC_HOST + imgUrl);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Intent intent = new Intent(context, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 3000);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_startadver_img:
                timer.cancel();
                Intent intent2 = new Intent(context, AdverWeb.class);
                intent2.putExtra("url", linkUrl);
                startActivity(intent2);
                finish();
                break;
            case R.id.tv_startadver_skip:
                timer.cancel();
                Intent intent = new Intent(context, MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}
